import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.sql.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;



public class SThreadLogin extends Thread{

	 private Socket socket = null;
	 private String Information = null;

	    public SThreadLogin(Socket socket, String Info) 
	    {
		super("Login Thread");
		this.socket = socket;
		this.Information=Info;
}

	    public void run() {

	    	String information=null, username=null, ip=null, pass=null;
	    	int pointer=0;
	    	try {
	    		
	    	    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
	    	    BufferedReader in = new BufferedReader(
	    				    new InputStreamReader(
	    				    socket.getInputStream()));

	    	    try
	    	    {
	    	    information=Information;
	    	    information=information.substring(2);
	    	    pointer=information.indexOf("|");
	    	    username=information.substring(0, pointer);
	    	    information=information.substring(pointer+1);
	    	    pointer=information.indexOf("|");
	    	    pass=information.substring(0, pointer);
	    	    information=information.substring(pointer+1);
	    	    pointer=information.indexOf("|");
	    	    ip=information.substring(0, pointer);
	    	    }
	    	    catch(Exception e )
	    	    {
	    	    	 out.write("!|1| " + e.toString() + "|" + '\n');
	    	    }
	    	    
	    	    try {
	    	    	Statement SQLstmt;
	    	    	ResultSet rs;
	    	    	Calendar cal = Calendar.getInstance();
	        	    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    	        Class.forName("com.mysql.jdbc.Driver");
	    	        String url = "jdbc:mysql://localhost:3306/mysql";

	    	        Connection con = DriverManager.getConnection(url,"root","admin");

	    	        SQLstmt = con.createStatement();
	    	        rs=SQLstmt.executeQuery("SELECT * FROM organize.user WHERE Username=" + "'" + username + "' AND HashedPass=" + "'" + pass + "'");
	    	        
	    	        if(rs.next())
	    	        {
	    	        	username=rs.getString("Username");
	    	        	SQLstmt.executeUpdate("UPDATE organize.user SET Active=1, IPAddress=" + "'" + ip + "' WHERE Username=" + "'" + username + "'");
	    	        	out.write("L|"+ username + "|" +  sdf.format(cal.getTime()) + "|" + '\n');
	    	        	System.out.println("LOGGED IN USER:"+username);
	    	        }
	    	        else
	    	        {
	    	        	out.write("!|5|"+ username + "|" +'\n');
	    	        }
	    	        rs.close();
	    	        SQLstmt.close();
		    	    con.close();
		    	    
	    	      }catch( Exception e ) {
	    	          out.write("!|11|Could not access database| " + e.toString()+ "|" +'\n');
	    	     }
 	    
	    	    out.close();
	    	    in.close();
	    	    socket.close();

	    	} catch (IOException e) {
	    	    e.printStackTrace();
	    	}
	        }
	    
}
